package youleche.com.sql.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import youleche.com.sql.business.BTableInfo;
import youleche.com.sql.model.MTableInfo;
import youleche.com.sql.servlet.common.SuperHttpServlet;
import youleche.com.util.JsonHelper;

/**
 * Servlet implementation class STableInfo
 */
public class STableInfoServlet extends SuperHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see 构造器
     */
    public STableInfoServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0:
				//添加
				this.add(request, response);
				break;
			case 1:
				//判断是否存在
				this.isExistTableName(request, response);
				break;
			case 2:
				//删除
				this.delete(request, response);
				break;
			case 3:
				//分页获取数据
				this.getlistForPage(request, response);
				break;
			case 4:
				//获取表数据总条数
				this.getTableInfoCount(request, response);
				break;
			case 5:
				//根据tableID获取表数据信息
				this.getTableInfoByTableId(request, response);
				break;
			case 6:
				//通过tableId更新说明
				this.updateTableDescByTableId(request, response);
				break;
			case 7:
				//分页查询
				this.getTableInfoListForQuery(request, response);
				break;
			case 8:
				//获取表数据总数(查询)
				this.getTableInfoCountForQuery(request, response);
				break;
		}
	}

	/**
	 * 获取表数据总数(查询)
	 * @param request 请求
	 * @param response 响应
	 */
	public void getTableInfoCountForQuery(HttpServletRequest request, HttpServletResponse response){
		BTableInfo tableBusi = new BTableInfo();
		try {
			
			Map<String, String> condition = new HashMap<String, String>();
			if(!"".equals(request.getParameter("tableName"))){
				condition.put("tableName", request.getParameter("tableName"));
			}
			
			if(!"".equals(request.getParameter("createUser"))){
				condition.put("createUser", request.getParameter("createUser"));
			}
			
			super.response(response, tableBusi.getTableInfoCountForQuery(condition));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 分页
	 * @param request 请求
	 * @param response 响应
	 */
	private void getlistForPage(HttpServletRequest request, HttpServletResponse response){
		BTableInfo tableBusi = new BTableInfo();
		try {
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));
			Integer startIndex = (Integer.parseInt(request.getParameter("pageIndex")) - 1) * pageSize ;
			JsonHelper.printObjectListToJsonString(response, tableBusi.getTableInfoListForPage(startIndex.toString(), ((Integer)pageSize).toString()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除
	 * @param request 请求
	 * @param response 响应
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response){
		BTableInfo tableBusi = new BTableInfo();
		try {
			super.response(response, tableBusi.deleteTableInfoById(request.getParameter("tableId")) == true ? "1" : "0");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据tableID获取表数据信息
	 * @param request 请求
	 * @param response 响应
	 */
	public void getTableInfoByTableId(HttpServletRequest request, HttpServletResponse response){
		BTableInfo tableBusi = new BTableInfo();
		try {			
			JsonHelper.printObjectToJsonString(response, tableBusi.getTableInfoByTableId(request.getParameter("tableId")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 新增
	 * @param request 请求
	 * @param response 响应
	 */
	private void add(HttpServletRequest request, HttpServletResponse response){
		BTableInfo tableBusi = new BTableInfo();
		try {
			JSONObject jsonObject = JsonHelper.readJSONObject(request);
			MTableInfo tableInfo = 	(MTableInfo)JSONObject.toBean(jsonObject, MTableInfo.class);
			super.response(response, ((Integer)tableBusi.addTableInfo(tableInfo)).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 判断表名是否存在
	 * @param request 请求
	 * @param response 响应
	 */
	private void isExistTableName(HttpServletRequest request, HttpServletResponse response){
		BTableInfo tableBusi = new BTableInfo();
		String tableName = request.getParameter("tableName");
		try {
			super.response(response, tableBusi.isExistTableName(tableName) == true ? "1" : "0");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取表数据总条数
	 * @param request 请求
	 * @param response 响应
	 */
	public void getTableInfoCount(HttpServletRequest request, HttpServletResponse response){
		BTableInfo tableBusi = new BTableInfo();
		try {
			super.response(response, tableBusi.getTableInfoCount());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 通过tableId更新说明
	 * @param request 请求
	 * @param response 响应
	 */
	public void updateTableDescByTableId(HttpServletRequest request, HttpServletResponse response){
		BTableInfo tableBusi = new BTableInfo();
		try {
			JSONObject jsonObject = JsonHelper.readJSONObject(request);
			MTableInfo tableInfo = 	(MTableInfo)JSONObject.toBean(jsonObject, MTableInfo.class);
			super.response(response, tableBusi.updateTableDescByTableId(tableInfo.getTableId(), tableInfo.getTableDesc()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 分页查询
	 * @param condition 查询条件
	 * @param request 请求
	 * @param response 响应
	 */
	public void getTableInfoListForQuery(HttpServletRequest request, HttpServletResponse response){
		BTableInfo tableBusi = new BTableInfo();
		try {
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));
			Integer startIndex = (Integer.parseInt(request.getParameter("pageIndex")) - 1) * pageSize ;
			Map<String, String> condition = new HashMap<String, String>();
			if(!"".equals(request.getParameter("tableName"))){
				condition.put("tableName", request.getParameter("tableName"));
			}
			
			if(!"".equals(request.getParameter("createUser"))){
				condition.put("createUser", request.getParameter("createUser"));
			}
			
			JsonHelper.printObjectListToJsonString(response, tableBusi.getTableInfoListForQuery(condition, startIndex.toString(), ((Integer)pageSize).toString()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
